package jaxp;

import java.io.File;
import java.net.MalformedURLException;

import javax.xml.transform.Source;
import javax.xml.validation.SchemaFactory;

import javax.xml.XMLConstants;
/*import javax.xml.transform.Source;*/
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;

import org.xml.sax.SAXException;

public class JaxpParser {

	public long validateJaxp(String xml, String xsd)
			throws MalformedURLException, SAXException {
		long startTime = System.nanoTime();
		File schemaFile = new File(xsd);
		Source xmlFile = new StreamSource(new File(xml));
		SchemaFactory schemaFactory = SchemaFactory
				.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		Schema schema = schemaFactory.newSchema(schemaFile);
		Validator validator = schema.newValidator();
		 long estimatedTime = System.nanoTime() - startTime;
         System.out.println("Total: "+ estimatedTime +" nanosegundos usando JAXP");
        
		try {
			validator.validate(xmlFile);
			System.out.println(xmlFile.getSystemId() + " is valid");
		} catch (SAXException e) {
			System.out.println(xmlFile.getSystemId() + " is NOT valid");
			System.out.println("Reason: " + e.getLocalizedMessage());
		} catch (Exception e) {
		}
		return estimatedTime;
	}
}